import AdminModel from '../models/Admin.js'
import { generateToken, verifyPassword } from '../utils/helper.js'

class AdminService
{
    static async login(data)
    {
        const { account, password } = data
        const res = await AdminModel.findOneBy({
            where: { account }
        }, ['id', 'password', 'status'])

        if (!res) {
            throw new global.errs.ServerError('用户名或密码错误', 10004)
        }

        const verify = await verifyPassword(password, res.password)
        if (!verify) {
            throw new global.errs.ServerError('用户名或密码错误', 10004)
        }

        // 颁布令牌
        const token = generateToken(res.id, 'endapi')

        return { token }
    }
}

export default AdminService